/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.phone.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.phone.entity.CommunityPhone;
import com.thinkgem.jeesite.modules.phone.service.CommunityPhoneService;

/**
 * 便民电话Controller
 * @author 张朋
 * @version 2016-09-07
 */
@Controller
@RequestMapping(value = "${adminPath}/phone/communityPhone")
public class CommunityPhoneController extends BaseController {

	@Autowired
	private CommunityPhoneService communityPhoneService;
	
	@ModelAttribute
	public CommunityPhone get(@RequestParam(required=false) String id) {
		CommunityPhone entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = communityPhoneService.get(id);
		}
		if (entity == null){
			entity = new CommunityPhone();
		}
		return entity;
	}
	
	@RequiresPermissions("phone:communityPhone:view")
	@RequestMapping(value = {"list", ""})
	public String list(CommunityPhone communityPhone, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CommunityPhone> page = communityPhoneService.findPage(new Page<CommunityPhone>(request, response), communityPhone); 
		model.addAttribute("page", page);
		return "modules/phone/communityPhoneList";
	}

	@RequiresPermissions("phone:communityPhone:view")
	@RequestMapping(value = "form")
	public String form(CommunityPhone communityPhone, Model model) {
		model.addAttribute("communityPhone", communityPhone);
		return "modules/phone/communityPhoneForm";
	}

	@RequiresPermissions("phone:communityPhone:edit")
	@RequestMapping(value = "save")
	public String save(CommunityPhone communityPhone, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, communityPhone)){
			return form(communityPhone, model);
		}
		communityPhoneService.save(communityPhone);
		addMessage(redirectAttributes, "保存信息成功");
		return "redirect:"+Global.getAdminPath()+"/phone/communityPhone/?repage";
	}
	
	@RequiresPermissions("phone:communityPhone:edit")
	@RequestMapping(value = "delete")
	public String delete(CommunityPhone communityPhone, RedirectAttributes redirectAttributes) {
		communityPhoneService.delete(communityPhone);
		addMessage(redirectAttributes, "删除信息成功");
		return "redirect:"+Global.getAdminPath()+"/phone/communityPhone/?repage";
	}

}